package com.cssy.xxkj.logic.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.byd.acegiext.domain.User;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.logic.entity.LogicWarehouseInventorySnapshotDO;
import com.cssy.xxkj.logic.entity.vo.LogicWarehouseInventoryVO;
import com.cssy.xxkj.logic.service.ILogicWarehouseInventoryService;
import com.cssy.xxkj.logic.service.IWhInventorySnapshotService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;
/**
 * 库存快照控制器
 * @author hf
 *
 */
@Controller
@RequestMapping("/logic/snapshot")
public class WhInventorySnapshotController extends BaseController<LogicWarehouseInventorySnapshotDO> {

	private static final Logger logger = Logger.getLogger(WhInventorySnapshotController.class);

	private static final String MANAGER = "/xxkj/logic/WhISnapshot/WHISnapshotManager";// 列表页面
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	@Resource(name = "logicWarehouseInventoryService")
	private ILogicWarehouseInventoryService logicWarehouseInventoryService;
	@Resource(name="whInventorySnapshotService")
	private IWhInventorySnapshotService whInventorySnapshotService;
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;

	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty(null);
		SearchResult result = whInventorySnapshotService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);

	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicWarehouseInventorySnapshotDO curAP, BindingResult result, Model model)
					throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();

		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();

		return bodyMap.modelMap;
	}

	@Override
	public String operationForm(HttpServletRequest request, String id,
			String flag, Model model) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	public void execute(){
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty(null);
		List<LogicWarehouseInventoryVO> lwi=logicWarehouseInventoryService.findForWareHouseStockByExample(scs).getResults();
		User user= new User("admin", "", false);
		String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		logger.info("仓库快照开始，time:"+time);
		for(LogicWarehouseInventoryVO ld:lwi){
			try {
				LogicWarehouseInventorySnapshotDO ls= new LogicWarehouseInventorySnapshotDO();
				ls.setId(IDCodeTypeUtil.getId());
				ls.setZd001WhId(ld.getZd001WhId());
				ls.setZd001WhCode(ld.getZd001WhCode());
				ls.setZd001WhName(ld.getZd001WhName());
				ls.setZd002WhLocationId(ld.getZd002WhLocationId());
				ls.setZd002WhLocationCode(ld.getZd002WhLocationCode());
				ls.setZd002WhLocationName(ld.getZd002WhLocationName());
				ls.setZd003MaterialsId(ld.getZd003MaterialsId());
				ls.setZd004MaterialsCode(ld.getZd004MaterialsCode());
				ls.setZd005MaterialsName(ld.getZd005MaterialsName());
				ls.setZd006MaterialsSpec(ld.getZd006MaterialsSpec());
				ls.setZd007MaterialsModel(ld.getZd007MaterialsModel());
				ls.setZd008Unit(ld.getZd008Unit());
				ls.setZd009Quantity(ld.getZd009Quantity());
				ls.setZd010BlockedStockQuantity(ld.getZd010BlockedStockQuantity());
				ls.setZd011TravelingQuantity(ld.getZd011TravelingQuantity());
				ls.setZd012QuarantineQuantity(ld.getZd012QuarantineQuantity());
				ls.setZd013GoodRetQuantity(ld.getZd013GoodRetQuantity());
				ls.setZd014RawRetQuantity(ld.getZd014RawRetQuantity());
				ls.setZd015WorkRetQuantity(ld.getZd015WorkRetQuantity());
				ls.setZd016DefectiveQuantity(ld.getZd016DefectiveQuantity());
				ls.setZd017OldPartsQuantity(ld.getZd017OldPartsQuantity());
				ls.setZd018OptimalQuantity(ld.getZd018OptimalQuantity());
				ls.setZd019OneQuantity(ld.getZd019OneQuantity());
				ls.setZd020Batch(ld.getZd020Batch());
				ls.setZd021Brand(ld.getZd021Brand());
				ls.setZd022MaterialsProDate(ld.getZd022MaterialsProDate());
				ls.setZd023MaterialsValidityDate(ld.getZd023MaterialsValidityDate());
				ls.setZd024Price(ld.getZd024Price());
				ls.setZd025Status(ld.getZd025Status());
				ls.setZd027SnapshotDate(new Date());
				ls.setZd026SnapshotCode(syCodeService.getCode("SS", "SS", SyCode.DATE_YYMMDD, 5));
				whInventorySnapshotService.save(ls, user);
			} catch (Exception e) {
				logger.error("仓库快照失败，time:"+time, e);
				e.printStackTrace();
			}
		}
		logger.info("仓库快照失败，time:"+time);

	}
}
